<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>

!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <script type="text/javascript">            
            function objetoAjax(){
                var XMLHttpRequestObject = false;
                if (window.XMLHttpRequest) {
                    XMLHttpRequestObject = new XMLHttpRequest();
                } else if (window.ActiveXObject) {
                    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
                }
                return XMLHttpRequestObject;
            }
            
            //associatibe array
            var person = new Array();
            person['name']="juan";
            person['age']="21";
            person['user']="myuser";

            //object
            var obj = new Object;
            obj.car="honda";
            obj.animal="cat";
            
            
            
            //object whit array
            var obj2 = new Object;
            obj2.car="toyota";
            obj2.animal=['cat','dog','lizard'];
            
            //object whit associative array
            var obj3 = new Object;
            obj3.car="toyota";
            obj3.persona=person;
            
            //array of objects
            var my_cars=[obj,obj2];
            
            function json_object(object){
                var json="{";
                for(property in object){
                    var value = object[property];                    
                    if(typeof(value)=="string"){                        
                        json +='"'+property+'":"'+value+'",';                        
                    }else{
                        if(!value[0]){                            
                            json+='"'+property+'":'+json_object(value)+',';
                        }else{
                            json+='"'+property+'":[';                            
                            for(prop in value)json+='"'+value[prop]+'",';
                                json = json.substr(0,json.length-1)+'],';                            
                        }
                    }
                }
                return json.substr(0,json.length-1)+'}';
            }    
            XMLHttpRequestObject=objetoAjax();
            if(XMLHttpRequestObject) {            
                XMLHttpRequestObject.onreadystatechange = function(){
                    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                        alert("r:     "+XMLHttpRequestObject.responseText);
                    }
                }
                XMLHttpRequestObject.open("POST", "json.php",true);
                XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                XMLHttpRequestObject.send("value="+json_object(my_cars));        
            }
           
            
            
        </script>
    </head>
    <body>
       
    </body>
</html>